package cn.iocoder.yudao.module.asasc.controller.admin.openapi;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.asasc.config.AsaConst;
import cn.iocoder.yudao.module.asasc.controller.admin.openapi.vo.RateRspVO;
import cn.iocoder.yudao.module.asasc.controller.admin.openapi.vo.RateVO;
import cn.iocoder.yudao.module.asasc.controller.admin.salesreport.vo.SalesReportSaveReqVO;
import cn.iocoder.yudao.module.asasc.service.campaign.CampaignDataApiService;
import cn.iocoder.yudao.module.asasc.service.salesreport.SalesReportService;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Slf4j
@RestController
@RequestMapping("/api/open")
public class OpenApiController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private CampaignDataApiService campaignDataApiService;
    @Resource
    private SalesReportService salesReportService;

    public static void main(String[] args) {
        String base = "";
        StringBuilder param = new StringBuilder();
        param.append("app_id=c0864bd429f443709362ab616b5e6278");
        if (StringUtil.isNotEmpty(base)) {
            param.append("&base=").append(base);
        }
        String url = "https://openexchangerates.org/api/latest.json";

        String res = HttpUtils.sendGet(url, param.toString());
        RateRspVO rateRspDTO = JSONUtil.toBean(res, RateRspVO.class);
        log.info(rateRspDTO.toString());
    }

    @GetMapping("/status")
    public String getStatus() {
        return "系统运行正常";
    }

    @PostMapping("/report-campaign-data")
    @Operation(summary = "上报 campaign data")
    public CommonResult<Map<String, Object>> postCampaignData(@RequestBody String body) {
        return success(campaignDataApiService.postDailyCampaigns(body));
    }

    @PostMapping("/report-sales-report")
    @Operation(summary = "上报 sales report")
    public CommonResult<Map<String, Object>> postSalesReport(@RequestBody List<SalesReportSaveReqVO> reqVOS) {
        return success(salesReportService.postSalesReport(reqVOS));
    }

    /**
     * 汇率接口-每天更新一次，系统定时任务调度
     *
     * @return
     */
    @Operation(summary = "rate exchange")
    @GetMapping("/rate")
    public CommonResult<Map<String, Object>> rate() {
        String url = "https://openexchangerates.org/api/latest.json";
        StringBuilder param = new StringBuilder();
        param.append("app_id=c0864bd429f443709362ab616b5e6278&base=usd");

        String res = HttpUtils.sendGet(url, param.toString());
        RateRspVO rateRspDTO = JSONUtil.toBean(res, RateRspVO.class);
        RateVO rates = rateRspDTO.getRates();
        Map<String, Object> rateMap = BeanUtil.beanToMap(rates);
        // 将 rateMap 中的值转换为字符串
        Map<String, String> stringRateMap = rateMap.entrySet().stream()
                .filter(entry -> entry.getValue() != null) // 过滤 null 值
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> entry.getValue().toString()
                ));
        stringRedisTemplate.opsForHash().putAll(AsaConst.rateKey, stringRateMap);

        Map<String, Object> map = Maps.newHashMap();
        map.put("data", res);

        return success(map);
    }
}
